package cn.pconline.autoclub.listener;
import org.gelivable.dao.DaoListener;
import org.gelivable.dao.GeliDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cn.pconline.autoclub.entity.User;


public class UserListener implements DaoListener {
    private static final Logger log = LoggerFactory.getLogger(UserListener.class);
    @Autowired
    private GeliDao geliDao;
    
    @SuppressWarnings("rawtypes")
    @Override
    public Class getEntityType() {
        return User.class;
    }

    @Override
    public void onEvent(Event event, Object entity) {
        User user = (User)entity;
        long userId = user.getUid();
        try {
            switch (event) {
            case PreUpdate:
                User cacheUser = geliDao.findDb(User.class, userId);
                if(cacheUser != null) {
                    System.out.println("\nget from db User-"+userId + ": " + geliDao.object2String(cacheUser));
                }
                System.out.println("update User-"+userId + ": " + geliDao.object2String(user));
                break;
            case PostUpdate:
                User updateUser = geliDao.find(User.class, userId);
                System.out.println("get updateUser User-"+userId + ": " + geliDao.object2String(updateUser)+"\n");
                break;
            default:
                break;
            }
        } catch (RuntimeException ex) {
            log.warn("user-{} envent<{}> error:{}", event, geliDao.object2String(user), ex.getMessage());
        }
    }
}
